package com.myapplication.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.myapplication.app.bean.TryJsonUtil;
import com.myapplication.app.date.TestDate;

/**
 * Created by tukela on 2015/3/2.
 */
public class LoginActivity  extends Activity implements View.OnClickListener{
    private int RECODE=0x10;
    private EditText editAccount;
    private EditText editPasswd;
    private Button btnLogin;
    private Button btnRegister;
    private Handler mHandler;
    private String account="";
    private String passwd="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mHandler = new Handler();
        editAccount= (EditText) findViewById(R.id.account);
        editPasswd= (EditText) findViewById(R.id.pd);
        btnLogin= (Button) findViewById(R.id.login);
        btnRegister= (Button) findViewById(R.id.register);
        btnLogin.setOnClickListener(this);
        btnRegister.setOnClickListener(this);
    }
    public boolean checkRight() {
        account = editAccount.getText().toString();
        passwd = editPasswd.getText().toString();
        if (account.equals("") || null == account) {
            showMessage("账户不能为空");
            return false;
        } else if (passwd.equals("") || null == passwd) {
            showMessage("密码不能为空");
            return false;
        }
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.login:
                if (checkRight())
                {
                    loadDate();
                }

                break;
            case R.id.register:
                Intent intent=new Intent(this,RegisterActivity.class);
                startActivityForResult(intent,RECODE);
                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       if (resultCode==RECODE)
       {
           this.finish();
       }
    }
    public void loadDate(){

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                loading();
            }
        }, 0);
    }
    private void loading(){
        RequestParams params = new RequestParams();
        params.addBodyParameter("name", "value");
        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST, MyApp.BSURL
                ,
                params,
                new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        //  testTextView.setText("reply: " + responseInfo.result);
                       String str=TryJsonUtil.isJsonRight(responseInfo.result);
                        if (str.contains("id"))
                        {
                            startActivity(new Intent(LoginActivity.this,HomeActivity.class));
                        }else {
                            showMessage("用户名或密码不对");
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                      //  showMessage("用户名或密码不对");
                        if (account.equals(passwd))
                        {
                            startActivity(new Intent(LoginActivity.this,HomeActivity.class));
                        }else {
                            showMessage("用户名或密码不对");
                        }
                    }
                });

    }
    public void showMessage(String str){
        Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
    }
}
